package com.devicemantra.coordinate.ds.tests;
import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


public class Upload extends HttpServlet 
{
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException,IOException 
	{
		
			
			/* Get all the Parameters */
			//String lContent = "";
			
			/*String lDeptName = req.getParameter("dept_names");
			System.out.println("Department name was " + lDeptName);
		    String lDeptDesc = req.getParameter("dept_desc");
		    String lDeptContactName = req.getParameter("dept_cont_name");
		    String lDeptContactMob = req.getParameter("dept_cont_mob");
		    String lDeptContactEmailId = req.getParameter("dept_cont_email");
		    
		     // lDeptName + "-" + lDeptDesc + "-"  + lDeptContactName + "-" +  lDeptContactMob + "-" + lDeptContactEmailId;
			ServletFileUpload lUploader = new ServletFileUpload();
		    FileItemIterator iter = lUploader.getItemIterator(req);
		    FileItemStream imageItem = iter.next();
		    InputStream imgStream = imageItem.openStream();
		    Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));
		    Logo lLogo = new Logo(imageItem.getName(),imageItem.getContentType() ,imageBlob );
		    System.out.println("File name is " + imageItem.getName());
		    
		    
		    PersistenceManager pm = PMF.get().getPersistenceManager();
		    System.out.println("Got the Persistent Manager");
		    pm.makePersistent(lLogo);
		    System.out.println("After make Persistent Manager");
		    pm.close();
		    System.out.println("After close() Persistent Manager");
		    System.out.println("Started upload now 4");

		    // respond to query
		    /*res.setContentType("image/jpeg");
		    res.getOutputStream().write(myImage.getImage().getBytes()); */
		try 
		{
			  ServletFileUpload upload = new ServletFileUpload();
		      res.setContentType("text/plain");
		      String lMessage = "";
		      //upload.
		      FileItemIterator iterator = upload.getItemIterator(req);
		      while (iterator.hasNext()) 
		      {
		        FileItemStream item = iterator.next();
		        InputStream stream = item.openStream();

		        if (item.isFormField())
		        {
		          lMessage = "Got a form field: " + item.getFieldName() + "<BR>";
		          System.out.println("Got a form field: " + item.getFieldName()  + " value is " );
		        } 
		        else 
		        {
		        	lMessage = "Got an uploaded file: " + item.getFieldName() +
		                      ", name = " + item.getName();
		        	System.out.println("Got an uploaded file: " + item.getFieldName() +
		                      ", name = " + item.getName());
		        }
		      }

		    res.getOutputStream().print("<script language=\"javascript\" type=\"text/javascript\">window.parent.test_parent(" + "\" <font color=green>" + lMessage +   "</font>\""  +  ")</script>");
		    System.out.println("Exiting upload now ");
		}
		catch(Exception e ) 
		{
			e.printStackTrace();
		}
	}
}
